import regeneratorRuntime from "regenerator-runtime";

function redirectTemporarily(where) {
    window.location.replace(where);
}

function getToken(){
    return getUrlVars().token;
}

function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for (var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function serializeForm(id) {
    let serializeObj = {};
    $(id).serializeArray().forEach((obj) => {
        if (serializeObj[obj.name]) {
            serializeObj[obj.name] += ',' + obj.value; //require escaping.
        } else {
            serializeObj[obj.name] = obj.value;
        }
    });
    return serializeObj;
}

async function authenticate() {
    await backend.auth.testToken(localStorage.token);
}

async function cacheMe(pck) {
    localStorage.token = pck.token;
    localStorage.uid = pck.uid;
}


var setFooter = function (idx) { };

var fillZero = function (v) { return v < 10 ? '0' + v : v; }

String.prototype.toLongTime = function () {
    var date = new Date(this);
    return date ?
        date.getFullYear() + '-' +
        fillZero(date.getMonth() + 1) + '-' +
        fillZero(date.getDate()) + ' ' +
        fillZero(date.getHours()) + ':' +
        fillZero(date.getMinutes()) + ':' +
        fillZero(date.getSeconds())
        : '';
};

String.prototype.toShortTime = function () {
    var date = new Date(this);
    return date ?
        date.getFullYear() + '-' +
        fillZero(date.getMonth() + 1) + '-' +
        fillZero(date.getDate())
        : '';
};

function getMyUID(){

}

export { getUrlVars, serializeForm, authenticate, cacheMe, redirectTemporarily ,getToken }

